package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.ShareVo;
import com.ruoyi.system.domain.bo.ShareBo;
import com.ruoyi.system.service.IShareService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分享
 *
 * @author ruoyi
 * @date 2023-08-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/share")
public class ShareController extends BaseController {

    private final IShareService iShareService;

    /**
     * 查询分享列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<ShareVo> list(ShareBo bo, PageQuery pageQuery) {
        return iShareService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出分享列表
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ShareBo bo, HttpServletResponse response) {
        List<ShareVo> list = iShareService.queryList(bo);
        ExcelUtil.exportExcel(list, "分享", ShareVo.class, response);
    }

    /**
     * 获取分享详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<ShareVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iShareService.queryById(id));
    }

    /**
     * 新增分享
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ShareBo bo) {
        return toAjax(iShareService.insertByBo(bo));
    }

    /**
     * 修改分享
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ShareBo bo) {
        return toAjax(iShareService.updateByBo(bo));
    }

    /**
     * 删除分享
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "分享", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iShareService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 生成取件码
     * @param bo
     * @return
     */
    @SaIgnore
    @PostMapping("genCode")
    public R<Long> genCode(@RequestBody ShareBo bo){
        Long code= iShareService.genCode(bo);
        return R.ok(code);
    }
}
